package com.woniu.his.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.sms.infra.model.Permission;
import com.woniu.his.sms.infra.mapper.PermissionMapper;
import com.woniu.his.sms.infra.model.RolePermissionRelation;
import com.woniu.his.sms.infra.model.Staff;
import com.woniu.his.sms.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.sms.service.RolePermissionRelationService;
import com.woniu.his.sms.service.StaffService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 准许（许可）表 服务实现类
 * </p>
 *
 * @author cb
 * @since 2022-11-25
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private RolePermissionRelationService rolePermissionRelationService;

    @Resource
    private StaffService staffService;

    @Override
    public List<String> getUrl(Long staffId) {
        Staff staff = staffService.getById(staffId);
        List<RolePermissionRelation> permissionRelations = rolePermissionRelationService.list(new QueryWrapper<RolePermissionRelation>().eq("role_id", staff.getRoleId()));
        List<Long> list = permissionRelations.stream().map(p -> p.getPermissionId()).collect(Collectors.toList());
        List<Permission> permissions = listByIds(list);
        List<String> urls = permissions.stream().map(p -> p.getUrl()).collect(Collectors.toList());
        return urls;
    }
}
